﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace WebShopData
{
    public class Order
    {
        public List<OrderRow> OrderRows = new List<OrderRow>();
        public int OrderID { get; set; }
        public int UserID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public string ZipCode { get; set; }
        public string City { get; set; }

        public void Load(SqlDataReader reader)
        {
            OrderID = int.Parse(reader["OrderID"].ToString());
            UserID = int.Parse(reader["UserID"].ToString());
            FirstName=reader["FirstName"].ToString();
            LastName = reader["LastName"].ToString();
            Email = reader["Email"].ToString();
            Address = reader["Address"].ToString();
            ZipCode = reader["ZipCode"].ToString();
            City = reader["City"].ToString();

           
        }

        public Order()
        {
        }

        public Order(int userId, string firstName, string lastName, string email, string address, string zipCode, string city, int orderId=0)
        {
            UserID = userId;
            FirstName = firstName;
            LastName = lastName;
            Email = email;
            Address = address;
            ZipCode = zipCode;
            City = city;
            OrderID = orderId;
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2} {3} {4} {5} {6} {7}", OrderID, UserID, FirstName, LastName, Email, Address, ZipCode, City );
        }
    }

    public static class Orders
    {
       public static int AddOrderToDB(Order order)
        {
            try
            {
                using (SqlConnection connection = DB.GetConnection())
                {
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandText = @"EXEC AddOrderHead @UserID = {0}, @FirstName = '{1}', @LastName = '{2}', @Email = '{3}', @Address = '{4}', @ZipCode = '{5}', @City = '{6}'; SELECT @@Identity";
                        command.CommandText = string.Format(command.CommandText, order.UserID, order.FirstName, order.LastName, order.Email, order.Address, order.ZipCode, order.City);
                        int orderID = 0;
                        int.TryParse(command.ExecuteScalar().ToString(), out orderID);

                        foreach (OrderRow orderRow in order.OrderRows)
                        {
                            OrderRows.AddOrderRow(orderRow, orderID);
                        }

                        return orderID;
                    }
                }
            }
            catch (Exception e)
            {
                DBException.AddException(e.Message, e.GetType().ToString());
                throw new Exception();
            }
        }

       public static int GetOrderID()
       {
           try
           {
               using (SqlConnection connection = DB.GetConnection())
               {
                   using (SqlCommand command = connection.CreateCommand())
                   {
                       command.CommandText = @"SELECT MAX(OrderID) OrderID FROM OrderHead";
                       int orderID = (int)command.ExecuteScalar();
                       return orderID;
                   }
               }
           }
           catch (Exception e)
           {
               DBException.AddException(e.Message, e.GetType().ToString());
               throw new Exception();
           }
       }

       public static Order GetOrder(int orderID)
       {
           try
           {
               var order = new Order();
               using (SqlConnection connection = DB.GetConnection())
               {
                   using (SqlCommand command = connection.CreateCommand())
                   {
                       command.CommandText = @"SELECT * FROM OrderHead WHERE OrderID = {0}";
                       command.CommandText = string.Format(command.CommandText, orderID);

                       SqlDataReader reader = command.ExecuteReader();
                       if (reader.Read())
                       {
                           order.Load(reader);
                       }
                   }
               }
               return order;
           }
           catch (Exception e)
           {
               DBException.AddException(e.Message, e.GetType().ToString());
               throw new Exception();
           }
       }
    }
}

